package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.dto.DeviceInfoDto;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        //1.获取该楼层所有的房间信息（里面包含了床位、设备等信息）
        List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        if(CollectionUtils.isEmpty(roomVos)){
            log.info("该楼层下没有含智能设备的房间");
            return roomVos;
        }
        //2.遍历所有的房间，找里面的设备,从Redis获取最新设备数据
        for (RoomVo roomVo : roomVos) {
            //获取房间里面所有的设备
            List<DeviceInfoDto> deviceVos = roomVo.getDeviceVos();
            //获取房间设备的数据
            for (DeviceInfoDto deviceVo : deviceVos) {
                //封装该设备最新数据,数据在Redis里面
                Object object = redisTemplate.opsForHash().get(CacheConstants.IOT_NEW_DATA, deviceVo.getIotId());
                if(object != null){
                    List<DeviceData> dataList = JSONUtil.toList(JSONUtil.toJsonStr(object), DeviceData.class);
                    deviceVo.setDeviceDataVos(dataList);
                }
            }
            //3.遍历该房间所有的床，找床上的设备，从Redis获取最新设备数据
            List<BedVo> bedVoList = roomVo.getBedVoList();
            for (BedVo bedVo : bedVoList) {
                List<DeviceInfoDto> bedDeviceVos = bedVo.getDeviceVos();
                for (DeviceInfoDto bedDeviceVo : bedDeviceVos) {
                    //封装该设备最新数据,数据在Redis里面
                    Object object = redisTemplate.opsForHash().get(CacheConstants.IOT_NEW_DATA, bedDeviceVo.getIotId());
                    if(object != null){
                        List<DeviceData> dataList = JSONUtil.toList(JSONUtil.toJsonStr(object), DeviceData.class);
                        bedDeviceVo.setDeviceDataVos(dataList);
                    }
                }
            }
        }
        return roomVos;
    }

    @Override
    public RoomVo getRoomPriceAndFloorById(Long roomId) {
        return roomMapper.getRoomPriceAndFloorById(roomId);
    }

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }
}
